﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HeathCareSystem.Tier.DTO;
using System.Data;

namespace HeathCareSystem
{
    public class ThuocDAO
    {
        DataProvider dp = new DataProvider();

        public static bool ThemThuocMoi(ThuocDTO th)
        {
            DataProvider dp = new DataProvider();
            string strSQL = string.Format("INSERT INTO Thuoc (TenThuoc, MaDonViThuoc)" +
                                     "VALUES (N'{0}',{1})", th.tenThuoc, th.maDonVi);
            return dp.ExecuteNonQuery(strSQL);
        }


        // Lấy toàn bộ Loại thuốc
       public static List<ThuocDTO> GetAll()
        {
            String strSql = "SELECT * FROM THUOC";
            DataProvider dp = new DataProvider();
            DataTable dtb = dp.ExecuteQuery(strSql);

            List<ThuocDTO> lstThuoc = new List<ThuocDTO>();

            foreach (DataRow dr in dtb.Rows)
            {
                ThuocDTO ThuocDTO = new ThuocDTO();
                //ThuocDTO.maThuoc = Convert int(dr["MaThuoc"].ToString());
                //ThuocDTO.tenThuoc = dr["TenThuoc"].ToString();
                //ThuocDTO.maDonVi = dr["MaDonVi"].ToString();
                lstThuoc.Add(ThuocDTO);
            }

            return lstThuoc;
        }

        // Xóa
        public static bool Delete(string strMaThuoc)
        {
            DataProvider a = new DataProvider();
            String strSql = "DELETE FROM THUOC WHERE MaThuoc = '" + strMaThuoc + "'";
           
            return a.ExecuteNonQuery(strSql);
        }

        // Update
        public static bool Update(ThuocDTO th)
        {
            DataProvider dp = new DataProvider();
            String strSql = "UPDATE THUOC SET TenThuoc = N'" + th.tenThuoc + "', MaDonViThuoc= "+th.maDonVi+"  WHERE MaThuoc = '" + th.maThuoc + "'";
            
            return dp.ExecuteNonQuery(strSql);
        }

        // Check Tên
        public static bool Check_Name(string strTenThuoc)
        {
            DataProvider dp = new DataProvider();
            String strSql = "SELECT * FROM THUOC WHERE TenThuoc = N'" + strTenThuoc + "'";
            DataTable dtb = dp.ExecuteQuery(strSql);

            if (dtb.Rows.Count > 0)
                return false;
            return true;
        }
    }

    
}